Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Jump Statements

Break in Java

Break

In Java, the break statement is a control statement that is used to alter the flow of a program by "breaking out" of certain control structures. It is primarily associated with loops (such as for, while, and do-while) and the switch statement. The break statement is a fundamental part of flow control and is used for specific scenarios where you need to exit a loop prematurely or terminate the execution of a switch statement. Here's a detailed explanation of the break statement in Java:

Breaking Out of Loops:

The most common use of the break statement is to exit a loop prematurely. When the break statement is encountered within a loop, the loop is terminated, and the program continues executing the code after the loop. This is particularly useful when you want to exit a loop based on a certain condition, even if the loop's condition hasn't been met.
Break Example in java
public class Main{ public static void main(String[] args) { for (int i = 1; i <= 10; i++) { if (i == 5) { break; // Exit the loop when i is 5 } System.out.println(i); } } }

Output

1 2 3 4
In this example, the loop will print numbers from 1 to 4 and then exit when i becomes 5.

Breaking Out of Nested Loops:

The break statement can also be used to exit from nested loops. When used within nested loops, it exits the innermost loop that contains it, allowing you to break out of multiple levels of nesting.
Find a position of a Number in java
public class Main{ public static void main(String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i * j == 6) { System.out.println("Found 6 at (" + i + ", " + j + ")"); break; // Exit the inner loop } } } } }

Output

Found 6 at (2, 3)
In this example, when the condition i * j == 6 is met, the inner loop is exited, but the outer loop continues.

Breaking Out of a switch Statement:

The break statement is used in a switch statement to exit the switch block. When a break is encountered within a case, it prevents the program from falling through and executing subsequent case statements. This behavior allows you to perform specific actions based on a value and then exit the switch statement.
Example of using break in Switch
public class Main{ public static void main(String[] args) { int dayOfWeek = 2; switch (dayOfWeek) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; // ... default: System.out.println("Unknown day"); break; } } }

Output

Tuesday
In this switch statement, when dayOfWeek is 2, it prints "Tuesday" and then exits the switch statement.

Using break with Labels:

Java also allows you to use the break statement with labels to exit nested loops by specifying which loop to break out of. This feature is less commonly used but can be helpful in situations involving deeply nested loops.
output with and without break in java
public class Main{ public static void main(String[] args) { switch (dayOfWeek) { case MONDAY: System.out.println("Today is Monday."); break; case TUESDAY: System.out.println("Today is Tuesday."); break; case WEDNESDAY: System.out.println("Today is Wednesday."); break; default: System.out.println("Today is not Monday, Tuesday, or Wednesday."); } } }

Output

Today is Monday. Today is Tuesday. Today is Wednesday. Today is not Monday, Tuesday, or Wednesday.
Without the break statement: If the break statement is not used This is because the switch statement will continue to execute until it reaches the default case. With the break statement: If the break statement is used, the code will only print the message for the matching case. For example, if the value of the variable dayOfWeek is MONDAY, the code will only print the message "Today is Monday." In summary, the break statement in Java is a control statement that allows you to exit loops prematurely or terminate the execution of a switch statement. It is a valuable tool for controlling the flow of your program and is commonly used in situations where you need to break out of loops based on specific conditions or requirements.

  📌TAGS

★break ★jump ★break statement ★control statement ★control in java ★break in java

Tutorials